import { NextApiRequest, NextApiResponse } from "next";
import { searchDatas } from "@/lib/userdata_database_method";



export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    // 设置 CORS 头部
    res.setHeader('Access-Control-Allow-Origin', '*'); // 允许所有来源

    const data = req.body;
    console.log(data);
    const sql = 'SELECT * FROM users_data WHERE username = ?';
    const params = [data.username];
    try {
        const dbresult = await searchDatas(sql, params);
        if (dbresult.length) {
            if (dbresult[0].password === data.password) {
                res.status(200).json({ login: true });
            }
            else {
                res.status(200).json({ login: false, type: "密码错误" });
            }
        } else {
            res.status(200).json({ login: false, type: "用户不存在" });
        }
    } catch (error) {
        console.log(error);
    }
}